package  {
import flash.display.*;
import flash.events.*;
import flash.display.MovieClip;
import src.*;

public class Game extends MovieClip {
	var boardCol:BoardCol;
	var boardHolder:MovieClip;
	var gp:GamePanel;
	var turn:String;	//pl or en
	var winner:String;
	var boardDim:Number;
	var logMov:MovieClip;
	var logStr:String = "";
	var isFullMode:Boolean;
	
	static var boardInfo:Array = new Array("Board 3x3","Board 4x4","Board 5x5");
	public function Game(){
		returnToMenu();
		return;
	}
	function startGameEv(e){
		//index is 0 for 3x3, 1 4x4, 2 5x5
		var index:Object;
		if(!isNaN(e)){
			index = e;
		}else{
			index = e.currentTarget.index;
			this.isFullMode = e.currentTarget.isFullMode;
		}
		
		clearMov();
		//starting the game
		boardDim = index+3;	//game dimension
		winner = "";
		logStr = "";
		
		//adding the boards
		boardHolder = Mi.addMovie(new MovieClip(),this);
		var bo:Board, boMov:BoardMov;
		boardCol = new BoardCol();
		for(var i=0;i<boardDim;i++){
			for(var j=0;j<boardDim;j++){
				boMov = new BoardMov();
				bo = new Board(i,j,boMov);
				boMov.setBoard(bo);
				boardCol.add(bo,i,j);
				Mi.addMovie(bo.disp,boardHolder,{x:i*80, y:j*80});
				//add the click event
				Mi.addEventC(bo.disp,boardClicked);
			}
		}
		//center the board
		boardHolder.x = (Mi.stageW - boardHolder.width)/2 + bo.disp.width/2;
		boardHolder.y = (Mi.stageH - boardHolder.height)/2 + bo.disp.height/2;
		
		//attach game panel
		gp = new GamePanel();
		gp.infoT2.text = (isFullMode? "Full AI Mode":"Lite AI Mode");
		Mi.addMovie(gp,this,{x:Mi.mid.x,y:15});
		Mi.addEventC(gp.backBtn,returnToMenu);
		Mi.addEventC(gp.reloadBtn,function(){
			startGameEv(index);
		});
		Mi.addEventC(gp.logBtn,showLog);
		setTurn(Math.random()<0.5? "pl":"en");
		showLog();
	}
	
	function showLog(e=null):void{
		if(logMov == null){
			//show
			logMov = Mi.addMovie(new LogMov(),this,{x:658,y:255});
			logMov.infoT.text = getLog;
			logMov.infoT.scrollV = logMov.infoT.maxScrollV;
			boardHolder.x -= 142;
		}else{
			//hide
			Mi.removeMovie(logMov);
			logMov = null;
			boardHolder.x += 142;
		}
	}
	function log(str:String):void{
		logStr += str+"\n";
		if(logMov != null){
			logMov.infoT.text = getLog;
			logMov.infoT.scrollV = logMov.infoT.maxScrollV;
		}
	}
	public function get getLog():String{
		return Mi.trim(logStr);
	}
	function returnToMenu(e=null):void{
		clearMov();
		Mi.addMovie(new titleMov(),this,{x:Mi.mid.x,y:50});
		for(var i=0;i<3;i++){
			var btn:MovieClip = Mi.addMovie(new globalBtn(),this,{x:Mi.mid.x-190,y:270+i*60});
			btn.index = i;
			btn.isFullMode = true;
			Mi.initBtn(btn,boardInfo[i], startGameEv);
		}
		for(i=0;i<3;i++){
			btn = Mi.addMovie(new globalBtn(),this,{x:Mi.mid.x+190,y:270+i*60});
			btn.index = i;
			btn.isFullMode = false;
			Mi.initBtn(btn,boardInfo[i], startGameEv);
		}
	}
	function boardClicked(e:MouseEvent):void{
		var bo = e.currentTarget.getBoard;
		if(turn != "pl" || bo.getOwner != "" || winner != "") return;
		boardCol.setOwner(bo,"pl");
		setTurn("en");
	}

	function setTurn(id:String):void{
		winner = boardCol.checkWinner();
		if(winner == "" && boardCol.getEmpty.length == 0){
			gp.setInfo("Game Tie!");
			gp.setColor(4);
			winner = "tie";
			return;
		}
		if(winner != ""){
			if(winner=="pl"){
				gp.setInfo("Player Wins!");
				gp.setColor(2);
			}else{
				gp.setInfo("Enemy Wins!");
				gp.setColor(3);
			}
			return;
		}
		gp.setInfo(id=="pl"? "Player's Turn":"Enemy's Turn");
		turn = id;
		if(id == "en"){
			gp.setInfo("Calculating Enemy AI..");
			//Mi.delay(gp,0.25,enemyRand);
			Mi.delay(gp,0.25,enemyAI);
		}
	}
	function enemyAI(e=null):void{
		var ai:Ai = new Ai(boardCol,isFullMode);
		log(ai.getLog);
		var bo:Board = ai.getResult;
		if(bo == null){
			//no solution found, enemy cannot win, just random
			log("Enemy clicks random board");
			enemyRand();
		}else{
			log("Enemy clicks "+bo.print());
			boardCol.setOwner(boardCol.find(bo),"en");
			setTurn("pl");
		}
	}
	function enemyRand(e=null):void{
		//enemy moves randomly (just testing)
		var bo:Board = boardCol.getRandEmpty;
		boardCol.setOwner(bo,"en");
		setTurn("pl");
	}
	function clearMov(){
		for(var i=this.numChildren-1;i>=0;i--){
			var mov:DisplayObject = this.getChildAt(i);
			this.removeChild(mov);
		}
		logMov = null;
	}
	
}
	
}
